import BTAction from "../../Base/BTAction";
import { NodeStatus, StateTypeEnum } from "../../../common/Enum";
import Boss1Tree from "../Tree/Boss1Tree";
import { GBoss1CD, G } from "../../../common/Global";
import { GameLogic } from "../../../Runtime/GameLogic";

export default class ActionImpact extends BTAction {

    constructor(
        private tree: Boss1Tree
    ) {
        super()
    }
    onStart(): void {
        const enemy = this.tree.enemy
        if (enemy.impactCD != null && enemy.impactCD >= GBoss1CD.impactCD) {// 开始蓄力
            enemy.impactCD = 0
            enemy.standCastingTime = 0
            enemy.state = StateTypeEnum.standCastingTime
        } else if (enemy.standCastingTime != null && enemy.state == StateTypeEnum.standCastingTime) {// 蓄力中
            enemy.standCastingTime += this.tree.state.dt
            if (enemy.standCastingTime >= GBoss1CD.standCastingTime) {// 蓄力完成 冲锋
                enemy.standCastingTime = 0
                const targetPos = GameLogic.Ins.getClosestActor(enemy.position, 10000)?.position
                const targetDir = { x: 0, y: 0 }
                if (targetPos) {
                    targetDir.x = targetPos.x - enemy.position.x
                    targetDir.y = targetPos.y - enemy.position.y
                }
                enemy.attackPos = targetDir
                enemy.state = StateTypeEnum.impact
            }
        } else if (enemy.during != null && enemy.state == StateTypeEnum.impact) {// 冲锋中
            enemy.during += this.tree.state.dt
            if (enemy.during > G.CastingTime) {// 冲锋结束
                enemy.state = StateTypeEnum.none
                enemy.during = 0
            }
        }

        super.onStart()
    }

    onEnd(): void {
        super.onEnd()
    }

    onUpdate(): NodeStatus {
        return NodeStatus.Success
    }
}